<?php

namespace common\models\datas;

use Yii;
use common\components\extenders\MenuManage;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use doumiaoduo\ueditor\ImageUploadMultipleTrait2;

/**
 * This is the model class for table "{{%position_info}}".
 *
 * @property integer $id
 * @property integer $position_id
 * @property integer $type
 * @property string $info
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $create_user_id
 */
class PositionInfo extends \common\components\bases\BaseModel
{
    use ImageUploadMultipleTrait2;

    public $picture;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%position_info}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['position_id', 'type', 'created_at', 'updated_at', 'create_user_id', 'branch_id'], 'integer'],
            [['info'], 'string'],
            [['position_id', 'branch_id'], 'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => \Yii::t('frontend', 'ID'),
            'position_id' => \Yii::t('frontend', '位置ID'),
            'type' => \Yii::t('frontend', '绑定类型'),
            'info' => \Yii::t('frontend', '内容'),
            'created_at' => \Yii::t('frontend', '创建时间'),
            'updated_at' => \Yii::t('frontend', '修改时间'),
            'create_user_id' => \Yii::t('frontend', '创建人'),
            'branch_id' => \Yii::t('frontend', '分店ID'),
            'imageList' => \Yii::t('frontend', '图集'),
            'picture'=>Yii::t('frontend', '图片'),

        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['type'] = function () {
            switch ($this->type) {
                case 0:
                    return \Yii::t('frontend', '文本');
                case 1:
                    return \Yii::t('frontend', '广告');
                case 2:
                    return \Yii::t('frontend', '小部件');
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['update_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'branch_name' => function () {
                return isset($this->site) ? $this->site->name : null;
            },
            'admin_name' => function () {
                return isset($this->admin) ? $this->admin->username : null;
            },
            'position_description' => function () {
                return isset($this->position) ? $this->position->position_description : null;
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'create_user_id']);
    }

    public function getPosition()
    {
        return $this->hasOne(Position::className(), ['id' => 'position_id']);
    }


    public function getSite()
    {
        return $this->hasOne(Site::className(), ['id' => 'branch_id']);
    }


}
